package com.jiepos.mpos.biz.outservice.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.ServiceInterLogDao;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_Inter_LogEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * @ClassName: ServiceInterLogService
 * @Description: 对外通道请求和应答日志服务
 * @author 孙永
 * @date 2018年2月28日 上午9:30:00
 */
@Service
public class ServiceInterLogService extends BaseService{
	
	@Autowired
	private ServiceInterLogDao serviceInterLogDao;
	
	public void save(App_Service_Inter_LogEntity logEntity){
		if(logEntity == null){
			return;
		}
		logEntity.setService_inter_uuid(IDFactoryUtil.getUUID());
		logEntity.setRec_st(SystemConstant.NORMAL_STATUS);
		logEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
		serviceInterLogDao.save(logEntity);
	}


	/**
	 * 查询信息列表json分页数据
	 * @param pagination
	 * @param appServiceInterLogEntity
	 * @return
	 */
	public Map getServiceInterLogList(HttpServletRequest request, Pagination pagination, App_Service_Inter_LogEntity appServiceInterLogEntity,App_Ins_InfEntity infEntity) {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT t2.ins_cd || '-' || t2.ins_cn_nm as inst_name, ")
                .append(" t1.inter_version, ")
                .append(" t1.inter_type, ")
                .append(" t1.mchnt_cd, ")
                .append(" t1.service_mchnt_cd, ")
                .append(" t1.service_term_id, ")
                .append(" t1.req_content, ")
                .append(" t1.req_biz_content, ")
                .append(" t1.res_content, ")
                .append(" t1.res_biz_content, ")
                .append(" t1.rsp_code || '-' || t1.rsp_msg as rsp_msg, ")
                .append(" T1.REQ_SN_NO, ")
                .append("to_char(t1.rec_crt_ts, 'yyyy-mm-dd hh24:mi:ss') rec_crt_ts")
                .append("   FROM t_app_service_inter_log t1 ")
                .append("   left join T_APP_INS_INF t2 ")
                .append("     on t1.ins_uuid = t2.ins_uuid where 1 = 1 ");
        List list = new ArrayList();

        //机构号
        if (!StringUtils.isBlank(infEntity.getIns_cd())) {
            sqlBuf.append( " and t2.ins_cd like ? " );
            list.add("%" + infEntity.getIns_cd() + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(infEntity.getIns_cn_nm())) {
            sqlBuf.append( " and t2.ins_cn_nm like ? " );
            list.add("%" + infEntity.getIns_cn_nm() + "%");
        }
        //会员编号
        if (!StringUtils.isBlank(appServiceInterLogEntity.getMchnt_cd())) {
            sqlBuf.append( " and t1.mchnt_cd like ? " );
            list.add("%" + appServiceInterLogEntity.getMchnt_cd() + "%");
        }
        //业务系统商户号
        if (!StringUtils.isBlank(appServiceInterLogEntity.getService_mchnt_cd())) {
            sqlBuf.append( " and t1.Service_mchnt_cd like ? " );
            list.add("%" + appServiceInterLogEntity.getService_mchnt_cd() + "%");
        }
        pagination.queryString = sqlBuf.toString();
        pagination = getInsAuthSql(request, pagination, "t1", null);
        pagination = this.getOrderSql(pagination, "t1");
        return  serviceInterLogDao.getPaginationMap(pagination,"t1",list);
	}
}
